FROM golang AS builder

LABEL stage=gobuilder


ARG PROJECT_BASE_DIR
ENV PROJECT_BASE_DIR=$PROJECT_BASE_DIR
ARG PROJECT_MAIN_FILE_NAME
ENV PROJECT_MAIN_FILE_NAME=$PROJECT_MAIN_FILE_NAME
ARG USE_CGO
ENV CGO_ENABLED $USE_CGO
ENV GOPROXY https://goproxy.cn,direct

WORKDIR /build

ADD go.mod .
ADD go.sum .
RUN go mod download
COPY . .
COPY ${PROJECT_BASE_DIR}/etc /app/etc
RUN go build -a -ldflags="-s -w -extldflags -static" -o /app/server ${PROJECT_BASE_DIR}/${PROJECT_MAIN_FILE_NAME}.go

FROM scratch

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai
ENV TZ Asia/Shanghai

WORKDIR /app
COPY --from=builder /app/server /app/server
COPY --from=builder /app/etc /app/etc

ENTRYPOINT ["./server"]

CMD ["-f", "etc/config.yaml"]
